import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../constants';
import TextInput from 'Shared/components/TextInput';
import {dealerType} from '../Enum';

const FormItem = Form.Item;
export const dealerConditions = {
    type: dealerType.总代
};

class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }

    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    onSelectDealer = data => {
        this.props.onConditionsChange('dealerId', data.id);
        this.props.onQueryDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    render() {
        const {initData} = this.props;
        const {formatMessage} = this.props.intl;

        return (
            <Form className="form-standard">
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.dealerCode',
                                defaultMessage: '总代编号'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextInput
                                name="dealerCode"
                                disabled={initData && initData.isDealerUser}
                                value={this.props.conditions.dealerCode}
                                onPressEnter={this.handleFilterPressEnter}
                                onChange={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.dealerName',
                                defaultMessage: '总代名称'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextInput
                                name="dealerName"
                                disabled={initData && initData.isDealerUser}
                                value={this.props.conditions.dealerName}
                                onPressEnter={this.handleFilterPressEnter}
                                onChange={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                    {!initData.isDealerUser && <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'queryPanel.form.shortCode',
                            defaultMessage: '简码'
                        })}{...FORM_OPTIONS.item}>
                            <TextInput
                                name="shortCode"
                                value={this.props.conditions.shortCode}
                                onPressEnter={this.handleFilterPressEnter}
                                onChange={this.handleFilterChange}/>
                        </FormItem>
                    </Col>}
                    {!initData.isDealerUser && <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'queryPanel.form.shortName',
                            defaultMessage: '简称'
                        })}{...FORM_OPTIONS.item}>
                            <TextInput
                                name="shortName"
                                value={this.props.conditions.shortName}
                                onPressEnter={this.handleFilterPressEnter}
                                onChange={this.handleFilterChange}/>
                        </FormItem>
                    </Col>}
                </Row>
                <Row>
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>{formatMessage({
                            id: 'queryPanel.btn.query',
                            defaultMessage: '查询'
                        })}</Button>
                        <Button key="clear" onClick={this.props.onResetClick}>{formatMessage({
                            id: 'queryPanel.btn.reset',
                            defaultMessage: '重置'
                        })}</Button>
                    </Col>
                </Row>
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    initData: PropTypes.object,
    intl: PropTypes.object,

};

import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, saveQueryDealer} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {injectIntl} from './intl';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getInitData = selectorFactory(['page', 'domainData', 'initData']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    queryDealer: getQueryDealer(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
    initData: getInitData(state)
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onQueryDealerChange: data => dispatch(saveQueryDealer(data)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
